<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
            confirmButtonText=""
            cancelButtonText=""
        >
            <el-form ref="formRef" :model="formData" label-width="84px">
                <el-form-item label="操作内容" prop="operatorName">
                    <el-input type="textarea" disabled  v-model="formData.operationContent" rows="3"/>
                </el-form-item>
<!--                <el-form-item label="操作人" prop="operatorName">-->
<!--                    <el-input v-model="formData.operatorName" placeholder="请输入操作人姓名" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="操作变量" prop="operationalVariable">-->
<!--                    <el-input v-model="formData.operationalVariable" placeholder="请输入操作变量" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="操作设备地址" prop="operatingEquipment">-->
<!--                    <el-input v-model="formData.operatingEquipment" placeholder="请输入操作设备地址" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="操作时间" prop="operatingTime">-->
<!--                    <el-input v-model="formData.operatingTime" placeholder="请输入操作时间" />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="创建时间" prop="creationTime">-->
<!--                    <el-date-picker-->
<!--                        class="flex-1 !flex"-->
<!--                        v-model="formData.creationTime"-->
<!--                        type="datetime"-->
<!--                        clearable-->
<!--                        value-format="YYYY-MM-DD hh:mm:ss"-->
<!--                        placeholder="请选择创建时间"-->
<!--                    />-->
<!--                </el-form-item>-->
<!--                <el-form-item label="0可用 1禁用" prop="status">-->
<!--                    <el-radio-group v-model="formData.status" placeholder="请选择0可用 1禁用">-->
<!--                        <el-radio label="0">请选择字典生成</el-radio>-->
<!--                    </el-radio-group>-->
<!--                </el-form-item>-->
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  logsEdit, logsAdd, logsDetail } from '@/api/logs'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '查看设备操作日志' : '新增设备操作日志'
})

const formData = reactive({
    id: '',
    operatorId: '',
    operatorName: '',
    operationalVariable: '',
    operatingEquipment: '',
    operatingTime: '',
    creationTime: '',
    status: '',
    operationContent: '',
})

const formRules = {
    id: [
        {
            required: true,
            message: '请输入主键',
            trigger: ['blur']
        }
    ],
    operatorId: [
        {
            required: true,
            message: '请输入操作人id',
            trigger: ['blur']
        }
    ],
    operatorName: [
        {
            required: true,
            message: '请输入操作人姓名',
            trigger: ['blur']
        }
    ],
    operationalVariable: [
        {
            required: true,
            message: '请输入操作变量',
            trigger: ['blur']
        }
    ],
    operatingEquipment: [
        {
            required: true,
            message: '请输入操作设备地址',
            trigger: ['blur']
        }
    ],
    operatingTime: [
        {
            required: true,
            message: '请输入操作时间',
            trigger: ['blur']
        }
    ],
    creationTime: [
        {
            required: true,
            message: '请选择创建时间',
            trigger: ['blur']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择0可用 1禁用',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await logsEdit(data) : await logsAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await logsDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
<style>
.el-button el-button--primary{

}
</style>
